package jp.shts.ohos.storyprogressbar.slice;

import jp.ohos.shts.storyprogressbar.ResourceTable;
import jp.shts.ohos.storiesprogressview.StoriesProgressView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.window.service.WindowManager;
import ohos.multimodalinput.event.TouchEvent;

public class MainAbilitySlice extends AbilitySlice implements StoriesProgressView.StoriesListener {
    private static final int PROGRESS_COUNT = 6;

    private static final long PROGRESS_DURATION = 3000L;

    private StoriesProgressView storiesProgressView;
    private Image image;

    private int counter = 0;
    private final int[] resources = new int[]{
            ResourceTable.Media_sample1,
            ResourceTable.Media_sample2,
            ResourceTable.Media_sample3,
            ResourceTable.Media_sample4,
            ResourceTable.Media_sample5,
            ResourceTable.Media_sample6,
    };

    private final long[] durations = new long[]{
            500L, 1000L, 1500L, 4000L, 5000L, 1000,
    };

    private long pressTime = 0L;

    private long limit = 500L;

    private Component.TouchEventListener onTouchListener = new Component.TouchEventListener() {
        @Override
        public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
            switch (touchEvent.getAction()) {
                case TouchEvent.PRIMARY_POINT_DOWN:
                    pressTime = System.currentTimeMillis();
                    storiesProgressView.pause();
                    return true;
                case TouchEvent.PRIMARY_POINT_UP:
                    long now = System.currentTimeMillis();
                    executeProgressView(component, now);
                    return limit < now - pressTime;
            }
            return true;
        }
    };

    @Override
    public void onStart(Intent intent) {
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);

        storiesProgressView = (StoriesProgressView) findComponentById(ResourceTable.Id_stories);
        storiesProgressView.setStoriesCount(PROGRESS_COUNT);
        storiesProgressView.setStoryDuration(PROGRESS_DURATION);
        // or
        // storiesProgressView.setStoriesCountWithDurations(durations);
        storiesProgressView.setStoriesListener(this);
        // storiesProgressView.startStories();
        counter = 2;
        storiesProgressView.startStories(counter);

        image = (Image) findComponentById(ResourceTable.Id_image);
        image.setPixelMap(resources[counter]);

        // bind reverse view
        Component reverse = findComponentById(ResourceTable.Id_reverse);
        reverse.setTouchEventListener(onTouchListener);

        // bind skip view
        Component skip = findComponentById(ResourceTable.Id_skip);
        skip.setTouchEventListener(onTouchListener);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onNext() {
        image.setPixelMap(resources[++counter]);
    }

    @Override
    public void onPrev() {
        if ((counter - 1) < 0) {
            return;
        }
        image.setPixelMap(resources[--counter]);
    }

    @Override
    public void onComplete() {
    }

    @Override
    protected void onStop() {
        // Very important !
        storiesProgressView.destroy();
        super.onStop();
    }

    /**
     * 处理 storiesProgressView 的触摸事件
     *
     * @param component 控件
     * @param now 时间戳
     */
    private void executeProgressView(Component component, long now) {
        long upTime = now - pressTime;
        if (upTime < 210) {
            int id = component.getId();
            if (id == ResourceTable.Id_reverse) {
                storiesProgressView.reverse();
            } else {
                storiesProgressView.skip();
            }
        } else {
            storiesProgressView.resume();
        }
    }

}
